const { expect } = require("chai");
const { ethers, upgrades } = require("hardhat");

describe("NftAuction Upgradeable", function () {
    let NftAuction, nftAuction, deployer, user1;

    beforeEach(async () => {
        [deployer, user1] = await ethers.getSigners();

        const SimpleERC721 = await ethers.getContractFactory("TestERC721");
        const erc721 = await SimpleERC721.deploy();
        console.log("ERC721 instance:", erc721);
        console.log("erc721.target:", erc721.target);
        const nftAddress = erc721.target ?? erc721.address;
        console.log("NFT合约地址:", nftAddress);
        // Mint 并等待交易确认
        const mintTx = await erc721.mint(deployer.address,1);
        const receipt = await mintTx.wait();
        const owner = await erc721.ownerOf(1);
        console.log("tokenId 1 所有者是:", owner);
        NftAuction = await ethers.getContractFactory("NftAuction");
        console.log("初始化参数 deployer.address:", deployer.address);
        console.log("初始化参数 duration:", 300);
        console.log("初始化参数 startPrice (wei):", ethers.parseEther("0.1").toString());
        console.log("初始化参数 nftAddress:", nftAddress);
        console.log("初始化参数 tokenId:", 1);

        nftAuction = await upgrades.deployProxy(NftAuction, [
            deployer.address,
            300,
            ethers.parseEther("0.1"),
            nftAddress,
            1
        ], { initializer: "initialize" });

        await nftAuction.waitForDeployment();

        //必须先给拍卖合约授权
        await erc721.connect(deployer).approve(nftAuction.address, 1);

        await nftAuction.connect(deployer).depositNFT();
    });




    it("should initialize correctly", async function () {
        expect(await nftAuction.seller()).to.equal(deployer.address);
        expect(await nftAuction.duration()).to.equal(300);
        expect(await nftAuction.startPrice()).to.equal(ethers.utils.parseEther("0.1"));
    });

    it("should accept bids and update highestBidder", async function () {
        await nftAuction.connect(user1).placeBid({ value: ethers.utils.parseEther("0.2") });
        expect(await nftAuction.highestBidder()).to.equal(user1.address);
        expect(await nftAuction.highestBid()).to.equal(ethers.utils.parseEther("0.2"));
    });
});
